home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / drawutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-05  |  10.4 KB  |  439 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     drawutil.c
  4.  
  5.     This reusable module contains miscellaneous drawing utility routines.
  6.     
  7.     Copyright © 1994, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <GestaltEqu.h>
  12. #include <Palettes.h>
  13.  
  14. #include "def.h"
  15. #include "drawutil.h"
  16.  
  17.  
  18.  
  19. static DeviceLoopDrawingUPP gDrawGrayRectOneDepthUPP = nil;
  20. static DeviceLoopDrawingUPP gDrawGrayRoundRectOneDepthUPP = nil;
  21. static DeviceLoopDrawingUPP gDrawColorPictOneDepthUPP = nil;
  22. static DeviceLoopDrawingUPP gFillColorPolyOneDepthUPP = nil;
  23.  
  24.  
  25.  
  26. /*----------------------------------------------------------------------------
  27.     HasColorQD 
  28.     
  29.     Check to see if color QuickDraw is available.
  30.     
  31.     Exit:    function result = true if we have color QD.
  32. ----------------------------------------------------------------------------*/
  33.  
  34. Boolean HasColorQD (void)
  35. {
  36.     OSErr err = noErr;
  37.     long quickdrawVersion;
  38.     static Boolean haveChecked = false;
  39.     static Boolean haveColorQD;
  40.     
  41.     if (!haveChecked) {
  42.         err = Gestalt(gestaltQuickdrawVersion, &quickdrawVersion);
  43.         haveColorQD = err == noErr && quickdrawVersion >= gestalt8BitQD;
  44.     }
  45.     return haveColorQD;
  46. }
  47.  
  48.  
  49.  
  50. /*----------------------------------------------------------------------------
  51.     SetPortTextStyle 
  52.     
  53.     Set the font, size, and style of the current port.
  54.             
  55.     Entry:    *style = text style record.
  56. ----------------------------------------------------------------------------*/
  57.  
  58. void SetPortTextStyle (TextStyle *style)
  59. {
  60.     TextFont(style->tsFont);
  61.     TextFace(style->tsFace);
  62.     TextSize(style->tsSize);
  63. }
  64.  
  65.  
  66.  
  67. /*----------------------------------------------------------------------------
  68.     GetPortTextStyle 
  69.     
  70.     Get the font, size, and style of the current port.
  71.             
  72.     Exit:    *style = text style record.
  73. ----------------------------------------------------------------------------*/
  74.  
  75. void GetPortTextStyle (TextStyle *style)
  76. {
  77.     style->tsFont = qd.thePort->txFont;
  78.     style->tsFace = qd.thePort->txFace;
  79.     style->tsSize = qd.thePort->txSize;
  80. }
  81.  
  82.  
  83.  
  84. /*----------------------------------------------------------------------------
  85.     DrawGrayRect 
  86.     
  87.     Draw a gray rectangle.
  88.             
  89.     Entry:    r = pointer to rectangle.
  90. ----------------------------------------------------------------------------*/
  91.  
  92. static pascal void DrawGrayRectOneDepth (short depth, short deviceFlags,
  93.     GDHandle targetDevice, long userData)
  94. {
  95.     RGBColor fg, bg, g;
  96.     
  97.     if (targetDevice != nil) {
  98.         GetForeColor(&fg);
  99.         GetBackColor(&bg);
  100.         g = fg;
  101.         if (GetGray(targetDevice, &bg, &g)) {
  102.             RGBForeColor(&g);
  103.             FrameRect((Rect*)userData);
  104.             RGBForeColor(&fg);
  105.             return;
  106.         }
  107.     }
  108.     PenPat(&qd.gray);
  109.     FrameRect((Rect*)userData);
  110.     PenPat(&qd.black);
  111. }
  112.  
  113. void DrawGrayRect (Rect *r)
  114. {
  115.     DeviceLoop(qd.thePort->visRgn, gDrawGrayRectOneDepthUPP, (long)r, 0);
  116. }
  117.  
  118.  
  119.  
  120. /*----------------------------------------------------------------------------
  121.     DrawGrayRoundRect 
  122.     
  123.     Draw a gray round rectangle.
  124.             
  125.     Entry:    r = pointer to rectangle.
  126.             ovalWidth = oval width.
  127.             ovalHeight = oval height.
  128. ----------------------------------------------------------------------------*/
  129.  
  130. typedef struct TDrawGrayRoundRectUserData {
  131.     Rect *r;
  132.     short ovalWidth;
  133.     short ovalHeight;
  134. } TDrawGrayRoundRectUserData;
  135.  
  136. static pascal void DrawGrayRoundRectOneDepth (short depth, short deviceFlags,
  137.     GDHandle targetDevice, long userData)
  138. {
  139.     RGBColor fg, bg, g;
  140.     TDrawGrayRoundRectUserData *ud;
  141.     
  142.     ud = (TDrawGrayRoundRectUserData*)userData;
  143.     if (targetDevice != nil) {
  144.         GetForeColor(&fg);
  145.         GetBackColor(&bg);
  146.         g = fg;
  147.         if (GetGray(targetDevice, &bg, &g)) {
  148.             RGBForeColor(&g);
  149.             FrameRoundRect(ud->r, ud->ovalWidth, ud->ovalHeight);
  150.             RGBForeColor(&fg);
  151.             return;
  152.         }
  153.     }
  154.     PenPat(&qd.gray);
  155.     FrameRoundRect(ud->r, ud->ovalWidth, ud->ovalHeight);
  156.     PenPat(&qd.black);
  157. }
  158.  
  159. void DrawGrayRoundRect (Rect *r, short ovalWidth, short ovalHeight)
  160. {
  161.     TDrawGrayRoundRectUserData ud;
  162.  
  163.     ud.r = r;
  164.     ud.ovalWidth = ovalWidth;
  165.     ud.ovalHeight = ovalHeight;
  166.     DeviceLoop(qd.thePort->visRgn, gDrawGrayRoundRectOneDepthUPP, (long)&ud, 0);
  167. }
  168.  
  169.  
  170.  
  171. /*----------------------------------------------------------------------------
  172.     DrawColorPict 
  173.     
  174.     Draw a color picture.
  175.             
  176.     Entry:    colorID = resource id of color version of PICT.
  177.             bwID = resource id of black and white version of PICT.
  178.             r = pointer to rectangle.
  179. ----------------------------------------------------------------------------*/
  180.  
  181. typedef struct TDrawColorPictUserData {
  182.     short colorID;
  183.     short bwID;
  184.     Rect *r;
  185. } TDrawColorPictUserData;
  186.  
  187. static pascal void DrawColorPictOneDepth (short depth, short deviceFlags,
  188.     GDHandle targetDevice, long userData)
  189. {
  190.     TDrawColorPictUserData *ud;
  191.     
  192.     ud = (TDrawColorPictUserData*)userData;
  193.     if (targetDevice != nil && depth > 2) {
  194.         DrawPicture(GetPicture(ud->colorID), ud->r);
  195.     } else {
  196.         DrawPicture(GetPicture(ud->bwID), ud->r);
  197.     }
  198. }
  199.  
  200. void DrawColorPict (short colorID, short bwID, Rect *r)
  201. {
  202.     TDrawColorPictUserData ud;
  203.  
  204.     ud.colorID = colorID;
  205.     ud.bwID = bwID;
  206.     ud.r = r;
  207.     DeviceLoop(qd.thePort->visRgn, gDrawColorPictOneDepthUPP, (long)&ud, 0);
  208. }
  209.  
  210.  
  211.  
  212. /*----------------------------------------------------------------------------
  213.     FillColorPoly 
  214.     
  215.     Fill a polygon in color.
  216.             
  217.     Entry:    poly = handle to polygon.
  218.             color = pointer to color.
  219.             pat = pattern.
  220. ----------------------------------------------------------------------------*/
  221.  
  222. typedef struct TFillColorPolyUserData {
  223.     PolyHandle poly;
  224.     RGBColor *color;
  225.     ConstPatternParam pat;
  226. } TFillColorPolyUserData;
  227.  
  228. static pascal void FillColorPolyOneDepth (short depth, short deviceFlags,
  229.     GDHandle targetDevice, long userData)
  230. {
  231.     TFillColorPolyUserData *ud;
  232.     RGBColor fgColor;
  233.     
  234.     ud = (TFillColorPolyUserData*)userData;
  235.     if (targetDevice != nil && depth > 2) {
  236.         GetForeColor(&fgColor);
  237.         RGBForeColor(ud->color);
  238.         FillPoly(ud->poly, ud->pat);
  239.         RGBForeColor(&fgColor);
  240.     }
  241. }
  242.  
  243. void FillColorPoly (PolyHandle poly, RGBColor *color, ConstPatternParam pat)
  244. {
  245.     TFillColorPolyUserData ud;
  246.  
  247.     ud.poly = poly;
  248.     ud.color = color;
  249.     ud.pat = pat;
  250.     DeviceLoop(qd.thePort->visRgn, gFillColorPolyOneDepthUPP, (long)&ud, 0);
  251. }
  252.  
  253.  
  254.  
  255. /*----------------------------------------------------------------------------
  256.     ComplementRgn 
  257.     
  258.     Complement a QuickDraw region.
  259.             
  260.     Entry:    rgn = handle to region.
  261.     
  262.     Exit:    Region complemented.
  263. ----------------------------------------------------------------------------*/
  264.  
  265. void ComplementRgn (RgnHandle rgn)
  266. {
  267.     static RgnHandle bigRgn = nil;
  268.     
  269.     if (bigRgn == nil) {
  270.         bigRgn = NewRgn();
  271.         SetRectRgn(bigRgn, -0x7fff, -0x7fff, 0x7fff, 0x7fff);
  272.     }
  273.     DiffRgn(bigRgn, rgn, rgn);
  274. }
  275.  
  276.  
  277.  
  278. /*----------------------------------------------------------------------------
  279.     LocalToGlobalRect 
  280.     
  281.     Convert a rectangle from local to global coordinates.
  282.             
  283.     Entry:    r = pointer to rectangle.
  284. ----------------------------------------------------------------------------*/
  285.  
  286. void LocalToGlobalRect (Rect *r)
  287. {
  288.     LocalToGlobal((Point*)&r->top);
  289.     LocalToGlobal((Point*)&r->bottom);
  290. }
  291.  
  292.  
  293.  
  294. /*----------------------------------------------------------------------------
  295.     GlobalToLocalRect 
  296.     
  297.     Convert a rectangle from global to local coordinates.
  298.             
  299.     Entry:    r = pointer to rectangle.
  300. ----------------------------------------------------------------------------*/
  301.  
  302. void GlobalToLocalRect (Rect *r)
  303. {
  304.     GlobalToLocal((Point*)&r->top);
  305.     GlobalToLocal((Point*)&r->bottom);
  306. }
  307.  
  308.  
  309.  
  310. /*----------------------------------------------------------------------------
  311.     LocalToGlobalRgn 
  312.     
  313.     Convert a region from local to global coordinates.
  314.             
  315.     Entry:    rgn = handle to region.
  316. ----------------------------------------------------------------------------*/
  317.  
  318. void LocalToGlobalRgn (RgnHandle rgn)
  319. {
  320.     Point where;
  321.     
  322.     SetPt(&where, 0, 0);
  323.     LocalToGlobal(&where);
  324.     OffsetRgn(rgn, where.h, where.v);
  325. }
  326.  
  327.  
  328.  
  329. /*----------------------------------------------------------------------------
  330.     GlobalToLocalRgn 
  331.     
  332.     Convert a region from global to local coordinates.
  333.             
  334.     Entry:    rgn = handle to region.
  335. ----------------------------------------------------------------------------*/
  336.  
  337. void GlobalToLocalRgn (RgnHandle rgn)
  338. {
  339.     Point where;
  340.     
  341.     SetPt(&where, 0, 0);
  342.     LocalToGlobal(&where);
  343.     OffsetRgn(rgn, -where.h, -where.v);
  344. }
  345.  
  346.  
  347.  
  348. /*----------------------------------------------------------------------------
  349.     GetFontNumber 
  350.     
  351.     Get the font number corresponding to a font name (adapted from Think Ref).
  352.             
  353.     Entry:    fontName = font name.
  354.     
  355.     Exit:    function result = true if font exists.
  356.             *fontNum = font number.
  357.             
  358.     If the font doesn't exist, the font number is set to Monaco, or to 
  359.     applFont if Monaco also doesn't exist.
  360. ----------------------------------------------------------------------------*/
  361.  
  362. Boolean GetFontNumber (Str255 fontName, short *fontNum)
  363. {
  364.     Str255 systemFontName;
  365.  
  366.     GetFNum(fontName, fontNum);
  367.     if (*fontNum != 0) return true;
  368.     GetFontName(0, systemFontName);
  369.     if (EqualString(fontName, systemFontName, false, false)) return true;
  370.     GetFNum("\pMonaco", fontNum);
  371.     if (*fontNum == 0) *fontNum = applFont;
  372.     return false;
  373. }
  374.  
  375.  
  376.  
  377. /*----------------------------------------------------------------------------
  378.     GetFontLineHeight 
  379.     
  380.     Get the line height in pixels of a window font.
  381.             
  382.     Entry:    wind = pointer to window.
  383.     
  384.     Exit:    function result = line height.
  385. ----------------------------------------------------------------------------*/
  386.  
  387. short GetFontLineHeight (WindowPtr wind)
  388. {
  389.     FontInfo fInfo;
  390.     GrafPtr port;
  391.  
  392.     GetPort(&port);
  393.     SetPort(wind);
  394.     GetFontInfo(&fInfo);
  395.     SetPort(port);
  396.     return fInfo.ascent + fInfo.descent + fInfo.leading;
  397. }
  398.  
  399.  
  400.  
  401. /*----------------------------------------------------------------------------
  402.     OutlineRegion 
  403.     
  404.     Change a region into a tracing of its border which is appropriate 
  405.     for normal dragging.
  406.             
  407.     Entry:    theRgn = handle to region.
  408.     
  409.     Exit:    Region changed to outline of region.
  410.     
  411.     From Apple "HFS Drag Sample" sample code.
  412. ----------------------------------------------------------------------------*/
  413.  
  414. void OutlineRegion (RgnHandle theRgn)
  415. {
  416.     RgnHandle tempRgn;
  417.     
  418.     tempRgn = NewRgn();
  419.     CopyRgn(theRgn, tempRgn);
  420.     InsetRgn(tempRgn, 1, 1);
  421.     DiffRgn(theRgn, tempRgn, theRgn);
  422.     DisposeRgn(tempRgn);
  423. }
  424.  
  425.  
  426.  
  427. /*----------------------------------------------------------------------------
  428.     drawutil_InitUPP
  429.     
  430.     Initialize UPPs.
  431. ----------------------------------------------------------------------------*/
  432.  
  433. void drawutil_InitUPP (void)
  434. {
  435.     gDrawGrayRectOneDepthUPP = NewDeviceLoopDrawingProc(DrawGrayRectOneDepth);
  436.     gDrawGrayRoundRectOneDepthUPP = NewDeviceLoopDrawingProc(DrawGrayRoundRectOneDepth);
  437.     gDrawColorPictOneDepthUPP = NewDeviceLoopDrawingProc(DrawColorPictOneDepth);
  438.     gFillColorPolyOneDepthUPP = NewDeviceLoopDrawingProc(FillColorPolyOneDepth);
  439. }